package com.neo.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.neo.util.DjsStringUtil;
import com.neo.util.RandomUserNameUtil;
import com.neo.util.ReadFromFile;


@Service
public class TransferLoanPeopleInfoService {

    protected static Logger logger= LoggerFactory.getLogger(TransferLoanPeopleInfoService.class); 
    
	@Autowired
    private JdbcTemplate jdbcTemplate;
	@Autowired
	private RandomUserNameUtil randomUserNameUtil;
	
	//CVS文件插入jx_loan_people_info 借款人信息
	public void insertInvUser() throws Exception {
		//List listCsv = ReadFromFile.readFileByLinesGbk(DjsStringUtil.IMPORT_FILE_PATH);
		List<String> listCsv = ReadFromFile.readFileByScanner(DjsStringUtil.IMPORT_FILE_PATH);
		
		int res=0;
		for (int i = 1; i < listCsv.size(); i++) {
		Map<String,Object> map = new HashMap();
			String str = listCsv.get(i);
			String[] s = str.split(",");

			map.put("LOAN_NO", s[0]);//借款流水号
			map.put("COMP_NAME", s[15]);//公司
			map.put("PRODUCT", s[11]);//产品
			//map.put("BOND_TYPE", s[]);//债权类型
			map.put("BORROWER_NAME", s[12]);//借款人姓名
			map.put("SEX", s[1]);//
			map.put("AGE", s[2]);//
			map.put("LOAN_AIM", s[25]);//借款目的
			map.put("ID_NUM", s[13]);//身份证号
			map.put("MOBILE", s[14]);//
			if ("等额本息".equals(s[22])) {
				map.put("REPAYMENT_WAY", "DEBX");//还款方式
			}
			map.put("PRIN_AMT", s[18]);//本金（元）
			map.put("RATE", s[20]);//利率（%）
			//map.put("INTEREST", s[]);//利息
			//map.put("LOAN_DAY", s[]);//期限（天）
			map.put("STATUS", "正常还款");//状态
			map.put("LOAN_DATE", s[4]);//借款日期
			
			updateByMap(map);
			logger.info("结果:"+res+"=="+i);
				
		}
		logger.info("结果文件数:"+res);
	}
	
	
	//智能拼接sql 
	private int updateByMap(Map<String,Object> map) {
		Object[] arg = new Object[map.keySet().size()];
		String sql = DjsStringUtil.generateInsertSql(map,arg,"jx_inv_borrower_message"); 
		int j = jdbcTemplate.update(sql,arg);
		return j;
	}
	
}
